package controllers

import (
	"ABlog/util"
	"github.com/astaxie/beego"
	"os"
)

type ApiExportController struct {
	beego.Controller
}

func (c *ApiExportController) Post() {
	c.Get()
}

func (c *ApiExportController) Get() {
	f, err := os.Open("./static/article/")
	defer f.Close()
	if nil != err {
		c.Ctx.WriteString(err.Error())
	}
	fis, err := f.Readdir(-1)
	if nil != err {
		c.Ctx.WriteString(err.Error())
	}
	// enum all
	var fileList []*os.File
	for _, v := range fis {
		file, err := os.Open("./static/article/" + v.Name())
		if nil != err {
			continue
		}
		fileList = append(fileList, file)
	}
	exportFileName := util.GenExportFileName()
	exportFilePath := "./static/tmp/" + exportFileName + ".zip"
	util.ZipIt("./static/article/", exportFilePath)
	// release resource
	for _, f := range fileList {
		f.Close()
	}
	// beego download file
	c.Ctx.Output.Download(exportFilePath, "export.zip")
}
